package main

import (
    "os"
    "fmt"
    "io/ioutil"
    "path/filepath"

    "github.com/urfave/cli"
)

func ListDir(dirPath string, deep int)(err error){
    // 如果 dirPath 为文件, 则 dirs 为空
    dirs, err := ioutil.ReadDir(dirPath)
    
    // 输出当前 path 信息
    for i:=1; i<deep;i++ {
        fmt.Printf("|    ")
    }
    fmt.Printf("|--- %s\n", filepath.Base(dirPath))

    // 下一级目录
    for _, dir := range dirs {
        ListDir( dirPath+"/"+dir.Name(), deep+1)
    }
    return 
}

func main() {
    app := cli.NewApp()
    app.Name = "tree"

    app.Usage = "list all file"
    app.Action = func(c *cli.Context) error {
        var dir string = "."
        if c.NArg() > 0 {
            dir = c.Args().Get(0)
        }
        ListDir(dir, 1)
        return nil
    }
    app.Run(os.Args)
}
